package com.yingjie.yingjiekitchen.controller;

import com.yingjie.commonbase.commons.ReturnObject;
import com.yingjie.commonbase.model.Kitchen;
import com.yingjie.commonsecurity.AuthUserContext;
import com.yingjie.yingjiekitchen.model.Food;
import com.yingjie.yingjiekitchen.model.FoodType;
import com.yingjie.yingjiekitchen.service.FoodService;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.Date;

@RestController
@RequestMapping("/food")
public class FoodController {

    @Resource
    private FoodService foodService;

    @PostMapping("/create")
    public ReturnObject create(@RequestBody Food food){
        Integer userId = AuthUserContext.get().getUserId();
        food.setUserId(userId);
        food.setCreatedAt(new Date());
        Integer result = foodService.create(food);
        if(result==0){
            return ReturnObject.ERROR("添加菜品失败");
        }
        return ReturnObject.SUCCESS(result);
    }

    //    获取用户下所有菜品列表
    @PostMapping("/getFoodListAll")
    public ReturnObject getFoodListAll(@RequestBody Kitchen kitchen){
        ArrayList<Food> foods = foodService.getFoodListAll(kitchen.getKitchenId());
        return ReturnObject.SUCCESS(foods);
    }

    // 修改菜品
    @PostMapping("/modify")
    public ReturnObject modify(@RequestBody Food food){
        food.setUpdatedAt(new Date());
        Integer result = foodService.modify(food);
        if(result==0){
            return ReturnObject.ERROR("修改菜品失败");
        }
        return ReturnObject.SUCCESS(result);
    }
    // 删除菜品
    @PostMapping("/delete")
    public ReturnObject delete(@RequestBody Food food){

        Integer result = foodService.delete(food.getFoodId());
        if(result==0){
            return ReturnObject.ERROR("删除菜品失败");
        }
        return ReturnObject.SUCCESS(result);
    }
}
